<html>
<head>
  <title>python 模块 ，面向对象编程基础</title>
  <basefont face="微软雅黑" size="2" />
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  <meta name="exporter-version" content="Evernote Windows/307027 (zh-CN, DDL); Windows/6.1.0 (Win32);"/>
  <style>
    body, td {
      font-family: 微软雅黑;
      font-size: 10pt;
    }
  </style>
</head>
<body>
<a name="636"/>
<h1>python 模块 ，面向对象编程基础</h1>

<div>
<span><div><h2><font style="font-size: 18pt; color: rgb(28, 51, 135);">导入模块</font></h2><div><br/></div><div><br/></div><h2><font style="font-size: 18pt; color: rgb(28, 51, 135);">动态导入模块</font></h2><div><span style="-en-paragraph: true;">利用</span><span style="-en-paragraph: true; color: #FF0000;">ImportError</span><span style="-en-paragraph: true;">错误，我们经常在Python中动态导入模块：</span></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial;-en-codeblock:true;"><div>try:</div><div>    from cStringIO import StringIO</div><div>except ImportError:</div><div>    from StringIO import StringIO</div></div><div style="-en-paragraph: true; margin-top: 1em; margin-bottom: 1em;"><span style="-en-paragraph: true;">上述代码先尝试从cStringIO导入，如果失败了（比如cStringIO没有被安装），再尝试从StringIO导入。这样，如果cStringIO模块存在，则我们将获得更快的运行速度，如果cStringIO不存在，则顶多代码运行速度会变慢，但不会影响代码的正常执行。</span></div><div><span style="-en-paragraph: true; font-weight: bold; color: #FF0000;">try </span><span style="-en-paragraph: true;">的作用是捕获错误，并在捕获到指定错误时执行 </span><span style="-en-paragraph: true; color: #FF0000; font-weight: bold;">except </span><span style="-en-paragraph: true;">语句。</span></div><div><br/></div><div><br/></div><h2><font style="font-size: 18pt; font-weight: normal; color: rgb(28, 51, 135);">python之使用__future__</font></h2><div><span style="-en-paragraph: true;">Python的新版本会引入新的功能，但是，实际上这些功能在上一个老版本中就已经存在了。要“试用”某一新的特性，就可以通过导入</span><span style="-en-paragraph: true; color: #FF0000;">__future__</span><span style="-en-paragraph: true;">模块的某些功能来实现。</span></div><div><span style="-en-paragraph: true;">但是，Python 3.x已经改进了整数的除法运算，“</span><span style="-en-paragraph: true; font-weight: bold;">/</span><span style="-en-paragraph: true;">”除将得到浮点数，“</span><span style="-en-paragraph: true; font-weight: bold;">//</span><span style="-en-paragraph: true;">”除才仍是整数：</span></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial;-en-codeblock:true;"><div>&gt;&gt;&gt; 10 / 3</div><div>3.3333333333333335</div><div>&gt;&gt;&gt; 10 // 3</div><div>3</div></div><div style="-en-paragraph: true; margin-top: 1em; margin-bottom: 1em;"><span style="-en-paragraph: true;">要在Python 2.7中引入3.x的除法规则，导入</span><span style="-en-paragraph: true; font-weight: bold;">__future__</span><span style="-en-paragraph: true;">的</span><span style="-en-paragraph: true; font-weight: bold;">division</span><span style="-en-paragraph: true;">：</span></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial;-en-codeblock:true;"><div>&gt;&gt;&gt; from __future__ import division</div><div>&gt;&gt;&gt; print 10 / 3</div><div>3.3333333333333335</div></div><div><span style="-en-paragraph: true;">当新版本的一个特性与旧版本不兼容时，该特性将会在旧版本中添加到</span><span style="-en-paragraph: true; font-weight: bold;">__future__</span><span style="-en-paragraph: true;">中，以便旧的代码能在旧版本中测试新特性。</span></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><h2 style="text-align: center;"><font color="#1C3387" style="font-size: 24pt;">定义类并创建实例</font></h2><div style="text-align: center;"><br/></div><div><span style="-en-paragraph: true;">在Python中，类通过 </span><span style="-en-paragraph: true; color: #FF0000; font-weight: bold;">class </span><span style="-en-paragraph: true;">关键字定义。以</span> <span style="-en-paragraph: true; font-weight: bold;">Person</span> <span style="-en-paragraph: true;">为例，定义一个</span><span style="-en-paragraph: true; font-weight: bold;">Person类</span><span style="-en-paragraph: true;">如下：</span></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial;-en-codeblock:true;"><div>class Person(object):</div><div>    pass</div></div><div style="-en-paragraph: true; margin-top: 1em; margin-bottom: 1em;"><span style="-en-paragraph: true;">按照</span> <span style="-en-paragraph: true; font-weight: bold;">Python</span> <span style="-en-paragraph: true;">的编程习惯，类名以</span><span style="-en-paragraph: true; color: #FF0000;">大写字母开头</span><span style="-en-paragraph: true;">，紧接着是(object)，表示该类是从哪个类继承下来的。类的继承将在后面的章节讲解，现在我们只需要简单地从</span><span style="-en-paragraph: true; font-weight: bold;">object</span><span style="-en-paragraph: true;">类继承。</span></div><div style="-en-paragraph: true; margin-top: 1em; margin-bottom: 1em;"><span style="-en-paragraph: true;">有了Person类的定义，就可以创建出具体的</span><span style="-en-paragraph: true; font-weight: bold;">xiaoming、xiaohong</span><span style="-en-paragraph: true;">等实例。创建实例使用 </span><span style="-en-paragraph: true; color: #FF0000; font-weight: bold;">类名+()</span><span style="-en-paragraph: true;">，类似函数调用的形式创建：</span></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial;-en-codeblock:true;"><div>xiaoming = Person()</div><div>xiaohong = Person()</div></div><div><br/></div><div><span style="-en-paragraph: true;">如何让</span><span style="-en-paragraph: true; color: #FF0000;">每个实例拥有各自不同的属性</span><span style="-en-paragraph: true;">？由于Python是动态语言，对每一个实例，都可以直接给他们的属性赋值，例如，给</span><span style="-en-paragraph: true; font-weight: bold;">xiaoming</span><span style="-en-paragraph: true;">这个实例加上</span><span style="-en-paragraph: true; font-weight: bold;">name、gender</span><span style="-en-paragraph: true;">和</span><span style="-en-paragraph: true; font-weight: bold;">birth</span><span style="-en-paragraph: true;">属性：</span></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial;-en-codeblock:true;"><div>xiaoming = Person()</div><div>xiaoming.name = 'Xiao Ming'</div><div>xiaoming.gender = 'Male'</div><div>xiaoming.birth = '1990-1-1'</div></div><div style="-en-paragraph: true; margin-top: 1em; margin-bottom: 1em;"><span style="-en-paragraph: true;">给</span><span style="-en-paragraph: true; font-weight: bold;">xiaohong</span><span style="-en-paragraph: true;">加上的属性不一定要和</span><span style="-en-paragraph: true; font-weight: bold;">xiaoming</span><span style="-en-paragraph: true;">相同：</span></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial;-en-codeblock:true;"><div>xiaohong = Person()</div><div>xiaohong.name = 'Xiao Hong'</div><div>xiaohong.school = 'No. 1 High School'</div><div>xiaohong.grade = 2</div></div><div><span style="line-height: 1.45;"><font style="font-size: 12pt; color: rgb(227, 0, 0);"><b><u>实例的属性可以像普通变量一样进行操作：</u></b></font></span><br/></div><div style="box-sizing: border-box; padding: 8px; font-family: Monaco, Menlo, Consolas, &quot;Courier New&quot;, monospace; font-size: 12px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; background-color: rgb(251, 250, 248); border: 1px solid rgba(0, 0, 0, 0.14902); background-position: initial initial; background-repeat: initial initial;-en-codeblock:true;"><div>xiaohong.grade = xiaohong.grade + 1</div></div><div><br/></div><h2><font color="#1C3387" style="font-size: 18pt; font-weight: normal;">初始化实例属性</font></h2><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div><div><br/></div></div></span>
</div></body></html> 